package com.sphd.emanu.activity.takeorder;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.choosefood.ChoosefoodActivity;
import com.sphd.emanu.adapter.TimeAdapter;

import com.sphd.emanu.mvp.MVPBaseActivity;
import com.wzutils.L;
import com.wzutils.TimeUtils;
import com.sphd.emanu.pojo.TimeEntity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 下单的界面
 *
 * @Author Hu
 * @Time 2017/4/19 -- 16:04.
 * @Email 87683202@qq.com
 */
public class TakeorderActivity extends MVPBaseActivity<TakeorderContract.View, TakeorderPresenter> implements TakeorderContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    @BindView(R.id.fab)
    Button fab;
    List<TimeEntity> listToday;
    List<TimeEntity> listTomorrow;
    long data = 5400000;//初始化时间
    private TimeAdapter timeAdapter;
    View view;
    private TextView today_time, tomorrow_time;
    int chooseType = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_takeorder);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                TimeEntity timeEntity = (TimeEntity) adapter.getItem(position);
                if (timeEntity.isCanSelected()) {
                    timeEntity.setSelected(true);
                    adapter.notifyItemChanged(position + 1);
                    for (int i = 0; i < (chooseType == 0 ? listToday.size() : listTomorrow.size()); i++) {
                        if (position != i) {
                            TimeEntity timeEntity2 = chooseType == 0 ? listToday.get(i) : listTomorrow.get(i);
                            if (timeEntity2.isSelected()) {
                                timeEntity2.setSelected(false);
                                adapter.notifyItemChanged(i + 1);
                            }
                        }
                    }
                } else {
                    toast("过时不能被选中了");
                }
            }
        });
    }

    private void initView() {
        setTitleBar(toolbar, "下单");
        initList();
        adapterinitDate(listToday,TimeUtils.stringTolong("11:00",TimeUtils.HOUR_MIN));
    }


    /**
     * 初始化heardview
     */
    private void initHeard() {
        view = View.inflate(context, R.layout.heard_take_order, null);
        today_time = (TextView) view.findViewById(R.id.today_time);
        tomorrow_time = (TextView) view.findViewById(R.id.tomorrow_time);

        today_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseType = 0;
                setTextColor(tomorrow_time, R.color.my_gray, R.drawable.bg_colorgray);
                setTextColor(today_time, R.color.white, R.drawable.bg_colorprimary);
                adapterinitDate(listToday,TimeUtils.stringTolong("11:00",TimeUtils.HOUR_MIN));
            }
        });
        tomorrow_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseType = 1;
                setTextColor(today_time, R.color.my_gray, R.drawable.bg_colorgray);
                setTextColor(tomorrow_time, R.color.white, R.drawable.bg_colorprimary);
                adapterinitDate(listTomorrow,TimeUtils.stringTolong(TimeUtils.longToString(5400000, TimeUtils.HOUR_MIN), TimeUtils.HOUR_MIN));
            }
        });
        timeAdapter.addHeaderView(view);
    }
    /*
    * 初始化时间，初始化列表
    * */
    private void adapterinitDate(List<TimeEntity> list,long time) {
        timeAdapter.setTime(time);
        timeAdapter.setNewData(list);
    }

    /*
    * 设置“今日”“明日”的字体颜色及边框
    * */
    private void setTextColor(TextView tv, int colorId, int bgId) {
        tv.setTextColor(ContextCompat.getColor(context, colorId));
        tv.setBackground(ContextCompat.getDrawable(context, bgId));
    }
    /*
    * 初始化 列表 采取双list方式单独保存 初始化完成后同时初始化适配器
    * */
    private void initList() {
        setGRecy(myRecycler, 4);
        listToday = new ArrayList<>();
        listTomorrow = new ArrayList<>();
        for (int i = 0; i < 22; i++) {
            data = data + 1000 * 60 * 30;
            if ((data >= 7200000 && data <= 19800000) || (data >= 32400000 && data <= 45000000)) {
                TimeEntity timeEntitytoday = new TimeEntity();
                TimeEntity timeEntitytomorrow = new TimeEntity();
                timeEntitytoday.setTimeString(TimeUtils.longToString(data, TimeUtils.HOUR_MIN));
                timeEntitytoday.setTimeLong(data);
                timeEntitytoday.setSelected(false);
                timeEntitytoday.setCanSelected(false);

                timeEntitytomorrow.setTimeString(TimeUtils.longToString(data, TimeUtils.HOUR_MIN));
                timeEntitytomorrow.setTimeLong(data);
                timeEntitytomorrow.setSelected(false);
                timeEntitytomorrow.setCanSelected(false);
                listToday.add(timeEntitytoday);
                listTomorrow.add(timeEntitytomorrow);
            }
        }
        timeAdapter = new TimeAdapter(R.layout.item_time, new ArrayList<TimeEntity>(), context);
        timeAdapter.setEmptyView(emptyView);
        myRecycler.setAdapter(timeAdapter);
        initHeard();
    }

    @OnClick(R.id.fab)
    public void onViewClicked() {
        for (TimeEntity timeEntity : chooseType == 0 ? listToday : listTomorrow) {
            L.e(timeEntity.toString());
            if (timeEntity.isSelected()){
                toast("ok");
                startActivity(new Intent(context, ChoosefoodActivity.class));
                return;
            }
        }
        toast("请选择送餐时间后下单");
    }

}
